package com.yootk.aware;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class DataBaseAwareBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
    private ApplicationContext context ;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext ;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Object config = this.context.getBean("dataBaseConfig") ;
        if (config == null){
            return bean ;
        }
        if (config instanceof  DataBaseConfig && bean instanceof IDataBaseAware){
            ((IDataBaseAware) bean).setDataBaseConfig((DataBaseConfig) config);
        }
        return bean ;
    }
}
